import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    proId: Number,
    cityId: Number,
    disId: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    address: '选择地址',
    multiArray: [],
    proIndex: 0,
    cityIndex: 0,
    disIndex: 0,
    multiIndex: [0, 0, 0]
  },


  ready: function() {
    this.init()
  },



  methods: {
    //确定
    bindMultiPickerChange: function(e) {
      const val = e.detail.value
      this.setData({
        multiIndex: val
      }) 
      this.resdetail()
    },

    //返回结果
    resdetail:function(){
      var address = [];
      var multiArray = this.data.multiArray;

      multiArray.map((item, i) => {
        var thisItem = this.mapCode(this.data.multiArray[i], this.data.multiIndex[i]) 
        address[i] = {
          name: thisItem.name,
          code: thisItem.code
        }
      }) 
      this.triggerEvent("addressDetail", address)
    },

    //列选择
    bindMultiPickerColumnChange: function(e) {
      var column = e.detail.column
      var index = e.detail.value
      var that = this;
      switch (column) {
        case 0:
          var pid = that.mapCode(that.data.multiArray[0], index).code;
          that.setData({
            proIndex: index,
            cityId: '',
            disId: '',
            multiIndex: [index, 0, 0]
          })
          that.getCityList(pid);
          return;
        case 1:
          var pid = this.mapCode(this.data.multiArray[1], index).code;
          this.setData({
            cityIndex: index,
            disId: '',
            multiIndex: [this.data.proIndex, index, 0]
          })

          this.getDisList(pid);
          return;
        case 2:
          var pid = this.mapCode(this.data.multiArray[2], index).code;
          this.setData({
            city: index,
            multiIndex: [this.data.proIndex, this.data.cityIndex, index]
          })
          return;
      } 

    },

    // 通过index变量查找对应对象
    mapCode: function(list, index) {
      var obj = {}
      list.map((item, i) => {
        if (index == i) {
          obj = item
        }
      })
      return obj

    },

    //通过code查找index
    findeCode: function(list, code) {
      var index = 0
      list.map((item, i) => {
        if (code == item.code) {
          index = i
        }
      })
      return index
    },


    //初始化数据
    init: function() {
      var that = this;
      this.getProList()
    },

    //获取省份列表
    getProList: function() { 
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading: false,
        data: {
          "pid": 0,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.data.multiArray[0] = res.list
        var proIndex = this.data.proId ? this.findeCode(res.list, this.data.proId) : 0
        this.setData({
          proIndex,
          multiArray: this.data.multiArray,
          multiIndex: [proIndex]
        })

        if (res.list && res.list.length > 0) {
          if (this.data.proId) {
            this.getCityList(this.data.proId)
          } else {
            this.getCityList(res.list[0].code)
          }
        }
      })
    },

    //获取城市
    getCityList: function(code) { 
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading: false,
        data: {
          "pid": code,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.data.multiArray[1] = res.list
        var cityIndex = this.data.cityId ? this.findeCode(res.list, this.data.cityId) : 0
        this.data.multiIndex = [this.data.proIndex, cityIndex]
        this.setData({
          cityIndex,
          multiArray: this.data.multiArray,
          multiIndex: this.data.multiIndex
        })
        if (res.list && res.list.length > 0) {
          if (this.data.cityId) {
            this.getDisList(this.data.cityId)
          } else {
            this.getDisList(res.list[0].code)
          }
        }

      })
    },


    //区域
    getDisList: function(code) { 
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading:false,
        data: {
          "pid": code,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.data.multiArray[2] = res.list
        var disIndex = this.data.disId ? this.findeCode(res.list, this.data.disId) : 0;
        var multiIndex = [this.data.proIndex, this.data.cityIndex, disIndex];
        
        this.setData({
          disIndex,
          multiArray: this.data.multiArray,
          multiIndex: multiIndex
        })
        this.resdetail()
      })
      
    }





  }
})